<template>
  <CustomDialog :drag-dialog="dialogInfo" style="z-index:1201;">
    <el-form ref="form" :model="form" :rules="rules" :disabled="!form.busPageInfo.editable" size="mini" label-width="160px">
      <el-form-item label="选择成员" prop="busMainData.userId">
        <el-input :readonly="true" v-model="form.busMainData.userName" placeholder="请选择选择成员">
          <el-button slot="append" icon="el-icon-search" @click="selectUser" />
        </el-input>
      </el-form-item>
      <el-form-item label="团队角色" prop="busMainData.memberRole">
        <el-select v-model="form.busMainData.memberRole" filterable clearable placeholder="团队角色">
          <el-option
            v-for="(item) in dicts.custom_member_role"
            :key="item.id"
            :label="item.dictName"
            :value="item.dictCode"/>
        </el-select>
      </el-form-item>
      <!-- <el-form-item label="成员权限, 对应数据字典 custom_member_role" prop="busMainData.memberPriv">
        <el-input v-model="form.busMainData.memberPriv" />
      </el-form-item>-->
      <el-form-item label="提成比例(合计100%)" prop="busMainData.memberRatio">
        <el-input-number v-model.number="form.busMainData.memberRatio" :controls="false" :precision="0" :min="0" :max="100"/>
      </el-form-item>
      <el-form-item label="最后更新时间" prop="busMainData.lastUpdateDate">
        <el-date-picker
          v-model="form.busMainData.lastUpdateDate"
          :disabled="true"
          placeholder="最后更新时间"
          type="date"
          size="mini"
          clearable
          style="width: 200px"
          class="filter-item" />
      </el-form-item>
    </el-form>
    <div v-if="form.busPageInfo.editable" slot="footer" class="modal-footer">
      <el-button type="text" @click="cancel">取消</el-button>
      <el-button v-permission="['SYS_ADMIN','COMMON_ADMIN','CRM_DETAIL_MEMBER_ALL','CRM_DETAIL_MEMBER_CREATE']" :loading="loading" type="primary" @click="doSave">保存</el-button>
    </div>
    <userSelectList ref="userSelectList" :multi_select="false" @userSelectCallback="userSelectCallback" />
  </CustomDialog>
</template>

<script>
import detailMember from '@/api/crm/DetailMember'
import userSelectList from '@/views/system/user/module/userSelectList'

export default {
  components: { userSelectList },
  dictTypes: ['custom_member_role'],
  props: {
    sup_this: {
      type: Object,
      required: true
    }
  },
  data() {
    return {
      isAdd: false,
      loading: false,
      style: 'width: 184px',
      dialogInfo: {
        name: 'detailMemberMngFormDialog',
        title: '成员管理',
        fullScreen: false,
        width: 50,
        height: 60
      },
      form: {
        busMainData: {
          id: '',
          enableFlag: 'Y',
          userName: ''
        },
        busPageInfo: {
          editable: false
        }
      },
      rules: {
        'busMainData.userId': [
          { required: true, message: '请选择用户', trigger: 'blur' }
        ],
        'busMainData.memberRole': [
          { required: true, message: '请选择团队角色', trigger: 'blur' }
        ],
        'busMainData.memberRatio': [
          { required: true, message: '请输入比例', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    cancel() {
      this.resetForm()
    },
    initDataOfBefore() {
    },
    displayDialog() {
      const { name } = this.dialogInfo
      this.$nextTick(() => {
        this.$modal.show(name)
      })
    },
    hiddenDialog() {
      const { name } = this.dialogInfo
      this.$nextTick(() => {
        this.$modal.hide(name)
      })
    },
    addData() {
      this.isAdd = true
      const data = { paramBean: {}}
      detailMember.toAdd(data).then(res => {
        this.displayDialog()
        this.form = res.data
        this.setObjectIdAndCode()
      }).catch(err => {
        console.log(err)
      })
    },
    copyData(mainId) {
      this.isAdd = true
      const data = { paramBean: { id: mainId }}
      detailMember.toCopy(data).then(res => {
        this.displayDialog()
        this.form = res.data
      }).catch(err => {
        console.log(err)
      })
    },
    viewMainData(id) {
      this.isAdd = false
      const data = { paramBean: { id: id }}
      detailMember.viewMainData(data).then(res => {
        this.displayDialog()
        this.form = res.data
      })
    },
    doSubmit() {
      this.doSingleSave(true)
    },
    doSave() {
      this.doSingleSave(false)
    },
    doSingleSave(isSubmit) {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          this.form.formSubmit = isSubmit
          this.loading = true
          if (this.isAdd) {
            this.doAdd()
          } else {
            this.doEdit()
          }
        }
      })
    },
    doAdd() {
      detailMember.addOrEdit(this.form).then(res => {
        this.resetForm()
        this.loading = false
        this.sup_this.init()
      }).catch(err => {
        this.loading = false
        console.log(err)
      })
    },
    doEdit() {
      detailMember.addOrEdit(this.form).then(res => {
        this.resetForm()
        this.loading = false
        this.sup_this.init()
      }).catch(err => {
        this.loading = false
        console.log(err)
      })
    },
    resetForm() {
      this.hiddenDialog()
      this.$refs['form'].resetFields()
    },
    changeData(busId) {
      const data = { paramBean: { id: busId }}
      detailMember.toChange(data).then(res => {
        this.displayDialog()
        this.form = res.data
      }).catch(err => {
        console.log(err)
      })
    },
    selectUser() {
      const _this = this.$refs.userSelectList
      _this.loadData()
      _this.dialog = true
    },
    userSelectCallback(selectedRow, userSelectObj) {
      this.form.busMainData.userId = selectedRow.id
      this.form.busMainData.userName = selectedRow.userName
      userSelectObj.cancel()
      userSelectObj.loading = false
    },
    setObjectIdAndCode() {
      const _this = this
      const busMainData = _this.sup_this.busMainData
      if (busMainData) {
        _this.form.busMainData.objectId = busMainData.id
        _this.form.busMainData.objectSysCode = busMainData.sysGenCode
        _this.form.busMainData.busModule = busMainData.busModule
      }
    }
  }
}
</script>
